package com.yangtzeu.jwc.utils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

/**
 * 图片加载完毕的显示动画类，第一次图片显示时候有个显示通话
 */
public class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

    static final List<String> displayedImages = Collections
            .synchronizedList(new LinkedList<String>());
    private ImageLoadingListener imListener;

    public AnimateFirstDisplayListener() {
    }

    public void setImageLoadingListener(ImageLoadingListener imListener) {
        this.imListener = imListener;
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        if (imListener != null) {
            imListener.onLoadingComplete(imageUri, view, loadedImage);
        }
        if (loadedImage != null) {
            Log.e("LogonActivity", "onLoadingComplete: imageUri  " + imageUri);
            ImageView imageView = (ImageView) view;
            boolean firstDisplay = !displayedImages.contains(imageUri);
            if (firstDisplay) {
                FadeInBitmapDisplayer.animate(imageView, 500);
                displayedImages.add(imageUri);
            }
//			File myCaptureFile = new File(folder, imageUri.substring(imageUri.lastIndexOf("/") + 1));
//			if (!myCaptureFile.exists()) {
//				BufferedOutputStream bos = null;
//				try {
//					L.d("缓存图片:"+myCaptureFile.getAbsolutePath());
//					myCaptureFile.createNewFile();
//					bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
//					loadedImage.compress(Bitmap.CompressFormat.JPEG, 80, bos);
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//				try {
//					if (bos != null)
//						bos.flush();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//				try {
//					if (bos != null)
//						bos.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
        }
    }
}